package cn.oms.manage.controller;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.JsonResult;
import cn.oms.manage.pojo.param.MedicineAddNewParam;
import cn.oms.manage.pojo.param.MedicineUpdateParam;
import cn.oms.manage.pojo.param.OrderAddParam;
import cn.oms.manage.pojo.param.OrderUpdateParam;
import cn.oms.manage.pojo.vo.DoctorNameListVO;
import cn.oms.manage.pojo.vo.MedicineListVO;
import cn.oms.manage.pojo.vo.OrderListVo;
import cn.oms.manage.pojo.vo.OrderStandardVO;
import cn.oms.manage.service.IDoctorService;
import cn.oms.manage.service.IOrderService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 说明：
 *
 * @author：ChenJJ
 * @date: 2023/6/16
 **/
@Slf4j
@RestController
@RequestMapping("/order")
@Validated
@Api(tags = "04. 预约管理模块")
public class OrderController {

    @Autowired
    private IOrderService orderService;



    // http://localhost:9999/order/add-new
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('/order/add')")
    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid OrderAddParam orderAddParam) {
        log.debug("开始处理【添加订单】的请求，参数：{}", orderAddParam);
        orderService.insert(orderAddParam);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/order/delete')")
    @ApiOperation("根据ID删除订单")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的订单ID值！") Long id) {
        log.debug("开始处理【根据ID删除订单】的请求，参数：{}", id);
        orderService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/order/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/order/update')")
    @ApiOperation("修改订单详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的订单ID值！") Long id,
                                           @Valid OrderUpdateParam orderUpdateParam) {
        log.debug("开始处理【修改订单详情】的请求，ID：{}，新数据：{}", id, orderUpdateParam);
        orderService.updateById(id, orderUpdateParam);
        return JsonResult.ok();
    }


    // http://localhost:9999/order/9527/update
    @PostMapping("/updateAutoState")
    @PreAuthorize("hasAuthority('/order/update')")
    @ApiOperation("自动修改订单状态")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateAutoState() {
        log.debug("开始准备查看预约时间是否过期！");
        orderService.updateState();
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/order/read')")
    @ApiOperation("根据ID查询订单详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "long")
    })
    public JsonResult<OrderStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的订单ID值！") Long id) {
        log.debug("开始处理【根据ID查询订单详情】的请求，参数：{}", id);
        OrderStandardVO queryResult = orderService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9999/medicines
    @GetMapping("")
    @PreAuthorize("hasAuthority('/order/read')")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<OrderListVo>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                     String queryType) {
        log.debug("开始处理【查询订单列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<OrderListVo> pageData;
        if ("all".equals(queryType)) {
            pageData = orderService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = orderService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

    // 根据用户名查询订单
    @GetMapping("/listByUsername")
    @PreAuthorize("hasAuthority('/order/read')")
    @ApiOperation("根据用户名查询订单")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String"),

    })
    public JsonResult<List<OrderListVo>> listByUsername( String username) {
        log.debug("开始根据用户名查询预约信息。");
        List<OrderListVo> orderListVos = orderService.listByUsername(username);
        return JsonResult.ok(orderListVos);
    }

    // 根据医生姓名查询订单
    @GetMapping("/listByDoctor")
    @PreAuthorize("hasAuthority('/order/read')")
    @ApiOperation("根据医生查询订单")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorName", value = "医生", dataType = "String"),

    })
    public JsonResult<List<OrderListVo>> listByDoctor( String doctorName) {
        log.debug("开始根据用户名查询预约信息。");
        List<OrderListVo> orderListVos = orderService.listByDoctor(doctorName);
        return JsonResult.ok(orderListVos);
    }

    // 根据订单状态查询订单
    @GetMapping("/listByState")
//    @PreAuthorize("hasAuthority('/medicines/read')")
    @ApiOperation("根据订单状态查询订单")
    @ApiOperationSupport(order = 450)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态", dataType = "String"),

    })
    public JsonResult<List<OrderListVo>> listByState( String state) {
        log.debug("开始根据用户名查询预约信息。");
        List<OrderListVo> orderListVos = orderService.listByState(state);
        return JsonResult.ok(orderListVos);
    }

    @Autowired
    IDoctorService doctorService;
    // 查询所有医生姓名
    @GetMapping("/listDoctorName")
//    @PreAuthorize("hasAuthority('/medicines/read')")
    @ApiOperation("查询医生的姓名")
    @ApiOperationSupport(order = 460)
    public JsonResult<List<DoctorNameListVO>> listDoctorName() {
        log.debug("开始查询医生姓名");
        List<DoctorNameListVO> doctorNames = doctorService.lisDoctorName();
        return JsonResult.ok(doctorNames);
    }

}
